(function (root, factory, plugin) {
    factory(root.jQuery, plugin);
})(window, function ($, plugin) {
    __DEFAULT__ = {
        url: '../assets/js/testData/orderNum.js',
        type: 'get',
        dataType: 'json'
    };
    __PROTOTYPE__ = {
        _init: function () {
            var containerHtml =
                "<div class=\"calendar-container\">" +
                "<table>" +
                "<thead>" +
                "<tr class=\"selected-control\">" +
                "<td class=\"left-arrow arrow\">&lt;" +
                "</td>" +
                "<td colspan=\"7\">" +
                "<span class=\"current-month-number\">" +
                "</span>" +
                "<span class=\"current-month\">June" +
                "</span>" +
                "<span class=\"current-year\">" +
                "</span>" +
                "</td>" +
                "<td class=\"right-arrow arrow\">&gt;" +
                "</td>" +
                "</tr>" +
                "<tr>" +
                "<td>&nbsp;</td>" +
                "<td>日</td>" +
                "<td>一</td>" +
                "<td>二</td>" +
                "<td>三</td>" +
                "<td>四</td>" +
                "<td>五</td>" +
                "<td>六</td>" +
                "<td>&nbsp;</td>" +
                "</tr>" +
                "</thead>" +
                "<tbody>" +
                "</tbody>" +
                "<tfoot>" +
                "<tr>" +
                "<td colspan=\"9\">" +
                "<div class=\"legend\">" +
                "<div class=\"legend-bar legend-bar-red\">" +
                "</div>" +
                "<div class=\"legend-text\">含驳回或查验状态业务" +
                "</div>" +
                "</div>" +
                "<div class=\"legend\">" +
                "<div class=\"legend-bar legend-bar-orange\">" +
                "</div>" +
                "<div class=\"legend-text\">正常状态业务" +
                "</div>" +
                "</div>" +
                "</td>" +
                "</tr>" +
                "</tfoot>" +
                "</table>" +
                "</div>";
            this.append(containerHtml);
            this.$curMonth = $(".current-month-number");
            this.$curMonthEng = $(".current-month");
            this.$curYear = $(".current-year");
            this.$leftArrow = $(".left-arrow");
            this.$rightArrow = $(".right-arrow");
            this.$arrow = $(".arrow")
            this.$tbody = $("tbody");
            this.enMonthArr =
            ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"];
            var curr = new Date();
            this.$curMonth.text(this.__addZero(curr.getMonth() + 1));
            this.$curYear.text(curr.getFullYear());
            this.$curMonthEng.text(this.enMonthArr[curr.getMonth()]);
            this.__generate();
            this.__bind();
        },
        __isLeap: function (year) {
            return year % 4 == 0 ? (year % 100 != 0 ? 1 : (year % 400 == 0 ? 1 : 0)) : 0;
        },
        __addZero: function (num) {
            return num < 10 ? "0" + num : num;
        },
        __generate: function () {
            var today = new Date();
            var y = this.$curYear.text();
            var m = parseInt(this.$curMonth.text()) - 1;
            var d = today.getDate();
            var firstday = new Date(y, m, 1);
            var dayOfWeek = firstday.getDay();
            var days_per_month = new Array(31, 28 + this.__isLeap(y), 31, 30, 31, 30, 31, 31, 30,
                                           31, 30, 31);
            var str_nums = Math.ceil((dayOfWeek + days_per_month[m]) / 7);
            for (var i = 0; i < str_nums; i++) {
                var html = "<tr><td>&nbsp;</td>";
                for (var k = 0; k < 7; k++) {
                    var idx = 7 * i + k;
                    var date = idx - dayOfWeek + 1;
                    (date <= 0 || date > days_per_month[m]) ? date = ' ' : date =
                                                                           this.__addZero(idx
                                                                                          - dayOfWeek
                                                                                          + 1);
                    (date == d && m == today.getMonth()) ? (html +=
                                                            '<td class="today" index="'
                                                            + parseInt(date) + '">' + date
                                                            + '</td>') : (html +=
                                                                          '<td index="'
                                                                          + parseInt(date) + '">'
                                                                          + date + '</td>');
                }
                html += "<td>&nbsp;</td></tr>";
                this.$tbody.append(html);
            }
            this.__load();
        },
        __load: function () {
            var _this = this;
            $.ajax({
                       url: _this.url,
                       type: _this.type,
                       data: {"date": _this.$curYear.text() + _this.$curMonth.text()},
                       dataType: _this.dataType,
                       success: function (data) {
                           /*var res=data.info;
                            if(res.length){
                            for(var i=0;i<res.length;i++){
                            $.each(_this.$tbody.find("td"),function(k,n){
                            if($(n).attr("index")==res[i].index){
                            console.log(res[i].type);
                            $(this).append('<span class="order-num'+' '+res[i].type+'">'+res[i].num+'</span>');
                            }
                            });
                            }
                            _this.$orderNum=$(".order-num");
                            _this.$orderNum.parent("td").css("cursor","pointer");
                            }
                            _this.__attach();   */
                           /*$(".order-num").remove();*/
                           if (data.error == "0" && (data.month == _this.$curMonth.text())) {
                               var res = data.content;
                               for (var prop in res) {
                                   $.each(_this.$tbody.find("td"), function (k, n) {
                                       if (_this.__addZero($(n).attr("index")) == prop) {
                                           console.log(res[prop].check_reject);
                                           var type = res[prop].check_reject ? "notPay" : "pay";
                                           $(this).append('<span class="order-num' + ' ' + type
                                                          + '">' + res[prop].count + '</span>');
                                           return false;
                                       }
                                   });
                               }
                               _this.$orderNum = $(".order-num");
                               _this.$orderNum.parent("td").css("cursor", "pointer");
                               _this.__attach();
                           } else {
                               /*layer.alert("查询错误", {
                                closeBtn: 0
                                }, function(){
                                WeixinJSBridge.call("closeWindow");
                                });*/
                               //window.location.href = "brokedown.html";
                           }
                           /*
                            _this.__attach();
                            */
                       },
                       error: function (a, b, c) {
                           console.log(b);
                       }

                   });
        },
        __attach: function () {
            var _this = this;
            this.$orderNum.parent("td").on("click", function () {
                var date = _this.__addZero(parseInt($(this).attr("index")));
                _this.curMonthVal = _this.__addZero(parseInt(_this.$curMonth.text()));
                _this.curYear = $(".current-year").text();
                var str = _this.curYear + "-" + _this.curMonthVal + "-" + date;
                console.log(str);
                var otherParams = function () {
                    var stringParams = "";
                    for (var key in _this.data) {
                        stringParams += key;
                        stringParams += "=";
                        stringParams += _this.data[key];
                    }
                    return stringParams;
                };
                /*alert(str);*/
                /*window.location.href = "result.html?date=" + str + "&" + otherParams();*/
                window.location.href = "result.html?date=" + str;
            })
        },
        __bind: function () {
            var _this = this;
            this.$arrow.on("click", function () {
                _this.curMonthVal = parseInt(_this.$curMonth.text());
                if ($(this).hasClass("left-arrow")) {
                    if (_this.curMonthVal > 1) {
                        _this.curMonthVal--;
                        _this.$curMonth.text(_this.__addZero(_this.curMonthVal));
                        _this.$curMonthEng.text(_this.enMonthArr[_this.curMonthVal - 1]);
                    } else if (_this.curMonthVal == 1) {
                        _this.$curMonth.text("12");
                        _this.$curMonthEng.text(_this.enMonthArr[parseInt(_this.$curMonth.text())
                                                                 - 1]);
                        _this.$curYear.text(parseInt(_this.$curYear.text()) - 1);
                    }
                } else {
                    if (_this.curMonthVal < 12) {
                        _this.curMonthVal++;
                        _this.$curMonth.text(_this.__addZero(_this.curMonthVal));
                        _this.$curMonthEng.text(_this.enMonthArr[_this.curMonthVal - 1]);
                    } else if (_this.curMonthVal == 12) {
                        _this.$curMonth.text("01");
                        _this.$curMonthEng.text(_this.enMonthArr[parseInt(_this.$curMonth.text())
                                                                 - 1]);
                        _this.$curYear.text(parseInt(_this.$curYear.text()) + 1);
                    }
                }
                _this.$tbody.find("tr").remove();
                _this.__generate();
            });
        }
    };
    $.fn[plugin] = function (ops) {
        $.extend(this, __PROTOTYPE__, __DEFAULT__, ops);
        this._init();
    }
}, "calendar");